package com.ruoyi.modularbusiness.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.modularbusiness.domain.AubitFieldbody;
import com.ruoyi.modularbusiness.domain.Formtype;
import com.ruoyi.modularbusiness.domain.VO.AubitTypeVo;
import com.ruoyi.modularbusiness.domain.VO.FormRtbVo;
import com.ruoyi.modularbusiness.mapper.AubitFieldbodyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.modularbusiness.mapper.FormrecordingMapper;
import com.ruoyi.modularbusiness.domain.Formrecording;
import com.ruoyi.modularbusiness.service.IFormrecordingService;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

/**
 * 表单记录Service业务层处理
 *
 * @author liuzheng
 * @date 2023-01-05
 */
@Service
public class FormrecordingServiceImpl implements IFormrecordingService {
    @Autowired
    private FormrecordingMapper formrecordingMapper;

    @Autowired
    private AubitFieldbodyMapper aubitFieldbodyMapper;


    @Override
    public List<FormRtbVo> GetFromrecording_body(Long id) {
        return formrecordingMapper.GetFromrecording_body(id);
    }

    @Override
    public List<AubitTypeVo> get_trid_notice(Long id) {
        return formrecordingMapper.get_trid_notice(id);
    }

    @Override
    public List<AubitTypeVo> getauditmloud(int topid) {
        return formrecordingMapper.getauditmloud(topid);
    }

    @Override
    public List<AubitTypeVo> getauditmloud2(AubitTypeVo afbd_number) {
        return formrecordingMapper.getauditmloud2(afbd_number);
    }

    @Override
    public List<FormRtbVo> auditmolud_sel(String code) {
        List<AubitFieldbody> lista = aubitFieldbodyMapper.selectAubitFieldbody_topid(code);
//        System.out.println(lista);
        if (lista.size() < 1) {
            return null;
        } else {
            try {
                List<FormRtbVo> list2 = new ArrayList<>();
                for (AubitFieldbody aubitFieldbody : lista) {
                    FormRtbVo formRtbVo = new FormRtbVo();
                    formRtbVo.setFrid(Integer.parseInt(aubitFieldbody.getTopid().toString()));
                    list2.add(formRtbVo);
                }
                List<FormRtbVo> list = formrecordingMapper.getformrough(list2);
                if (list.size() == 1) {
                    list.get(0).setFbid(Integer.parseInt(lista.get(0).getAfbdId().toString()));
                } else {
                    for (int j = 0; j < lista.size(); j++) {
                        list.get(j).setFbid(Integer.parseInt(lista.get(j).getAfbdId().toString()));
                    }
                }
                return list;
            } catch (Exception e) {
                return null;
            }
        }
    }

    @Override
    public List<FormRtbVo> getformrough(List<FormRtbVo> frid) {
        return formrecordingMapper.getformrough(frid);
    }

    @Override
    public List<FormRtbVo> get_baoan(Long id) {
        return formrecordingMapper.get_baoan(id);
    }

    @Override
    public List<Formrecording> selform_audit(String numbers) {
        return formrecordingMapper.selform_audit(numbers);
    }

    @Override
    public List<FormRtbVo> seluserform_all_state(int userid) {
        Formrecording formrecording = new Formrecording();
        String id = "" + userid;
        formrecording.setUserid(id);
        return formrecordingMapper.seluserform_all_state(formrecording);
    }

    /**
     * 查询表单内容
     *
     * @param formrecording 类条件
     */
    @Override
    public List<FormRtbVo> GetFormrecordingByFrid(Formrecording formrecording) {
        return formrecordingMapper.GetFormrecordingByFrid(formrecording);
    }

    /**
     * 查询表单记录
     *
     * @param frid 表单记录主键
     * @return 表单记录
     */
    @Override
    public Formrecording selectFormrecordingByFrid(Long frid) {
        return formrecordingMapper.selectFormrecordingByFrid(frid);
    }

    /**
     * 查询表单记录列表
     *
     * @param formrecordings 表单记录
     * @return 表单记录
     */
    private List<Formrecording> processFormRecordings(List<Formrecording> formrecordings) {
        for (Formrecording formrecording : formrecordings) {
            Formrecording formrecording1 = new Formrecording();
            formrecording1.setFrid(formrecording.getFrid());
            formrecording.setFormRtbVo(formrecordingMapper.GetFormrecordingByFrid(formrecording1));
        }
        return formrecordings;
    }

    @Override
    public List<Formrecording> selectFormrecordingList(Formrecording formrecording) {
        List<Formrecording> formrecordings = formrecordingMapper.selectFormrecordingList(formrecording);
        return processFormRecordings(formrecordings);
    }

    @Override
    public List<Formrecording> selectFormrecordingList2(Formrecording formrecording) {
        List<Formrecording> formrecordings = formrecordingMapper.selectFormrecordingList2(formrecording);
        return processFormRecordings(formrecordings);
    }

    /**
     * 新增表单记录
     *
     * @param formrecording 表单记录
     * @return 结果
     */
    @Override
    public int insertFormrecording(Formrecording formrecording) {
        formrecording.setCreateTime(DateUtils.getNowDate());
        return formrecordingMapper.insertFormrecording(formrecording);
    }

    /**
     * 修改表单记录
     *
     * @param formrecording 表单记录
     * @return 结果
     */
    @Override
    public int updateFormrecording(Formrecording formrecording) {
        formrecording.setUpdateTime(DateUtils.getNowDate());
        return formrecordingMapper.updateFormrecording(formrecording);
    }

    /**
     * 批量删除表单记录
     *
     * @param frids 需要删除的表单记录主键
     * @return 结果
     */
    @Override
    public int deleteFormrecordingByFrids(Long[] frids) {
        return formrecordingMapper.deleteFormrecordingByFrids(frids);
    }

    /**
     * 删除表单记录信息
     *
     * @param frid 表单记录主键
     * @return 结果
     */
    @Override
    public int deleteFormrecordingByFrid(Long frid) {
        return formrecordingMapper.deleteFormrecordingByFrid(frid);
    }
}
